package com.github.permissiondog.cancanpbackend.servlet;

import com.github.permissiondog.cancanpbackend.model.User;
import com.github.permissiondog.cancanpbackend.response.*;
import com.github.permissiondog.cancanpbackend.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int userId;
        try {
            userId = Integer.parseInt(req.getParameter("userId"));
        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("参数错误").build());
            return;
        }

        var user = UserService.getInstance().getUserById(userId);
        json(res, UserResponse.builder().ok(true).user(user).build());
    }
}
